package com.heima.controller;

import com.heima.pojo.dto.NodeQueryDTO;
import com.heima.pojo.entity.Business;
import com.heima.pojo.entity.Node;

import com.heima.pojo.entity.TbNode;
import com.heima.pojo.vo.TbSkuPageVO;

import com.heima.pojo.vo.VendingMachineVO;
import com.heima.service.NodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/vm-service")
public class NodeController {
    @Autowired
    private NodeService nodeService;

    @GetMapping("/node/search")
    public TbSkuPageVO search(NodeQueryDTO nodeQueryDTO) {
        return nodeService.search(nodeQueryDTO);
    }
    @GetMapping("/businessType")
    public List<Business>  searchBusiness(){
        return nodeService.searchBusiness();
    }
    @PostMapping("/node")
    public Boolean insertBusiness(@RequestBody  Node Node){
        return nodeService.insertBusiness(Node);
    }

    @GetMapping("/node/vmList/{nodeId}")
    public ResponseEntity<List<VendingMachineVO>> getVmDetailByNodeId(
            @PathVariable("nodeId") String nodeId) {

        // 调用服务查询
        List<VendingMachineVO> vmVOList = nodeService.getVmByNodeId(nodeId);

        // 响应处理：有数据返回200，无数据返回404
        if (vmVOList != null && !vmVOList.isEmpty()) {
            return ResponseEntity.ok(vmVOList);
        } else {
            return ResponseEntity.notFound().build();
        }
    }
    @PutMapping("/node/{nodeId}")
    public Boolean update(@PathVariable String nodeId, @RequestBody TbNode tbNode){
        return nodeService.update(nodeId,tbNode);
    }
    @DeleteMapping("/node/{nodeId}")
    public Boolean deleteNode(@PathVariable String nodeId){
        return nodeService.deleteNode(nodeId);

    }
}